/**
 * Created by cb on 15/3/31.
 */
window.fTabCut = function (e, callback) {
    var $_cont, $_nav, index, target;
    e = e || widnow.e;
    if ($(e.target).hasClass('tab-b') || $(e.target).parent().hasClass('tab-b')) {
        target = $(e.target).hasClass('tab-b') ? $(e.target) : $(e.target).parent();
        if (target.hasClass('current')) {
            return false;
        }
        $_nav = target.parents('.tabs-nav').find('.tab-b');
        $_cont = $(this).children('.tabs-cont').children('.tab-c');
        $_nav.removeClass('current');
        target.addClass('current');
        index = target.index();
        if (callback)
            callback(target);
        return $_cont.removeClass('current').eq(index).addClass('current');
    }
};
$(function () {
    $(' .member-menu').mouseover(function () {
        return $(this).addClass('hover');
    }).mouseout(function () {
        return $(this).removeClass('hover');
    });
    var nCliHeight = $('.wrap-ct').eq(0).outerHeight(true) * $('.wrap-ct').length;
    $('.category li.c-item').hover(function () {
        var i = $(this).index();
        var $_detail = $(this).find('.c-detail'), nH = $(this).find('.wrap-ct').outerHeight(true), nOh = $_detail.outerHeight(true);
        if (nOh > nCliHeight || (nOh >= nH * (i + 1))) {
            $_detail.css('top', -nH * i);
        }
        else {
            var nWinH = $(window).height() + $(window).scrollTop(), nP = $(this).offset().top + nOh;
            if (nWinH < nP) {
                $_detail.css('top', -(nOh - nH));
            }
            else {
                $_detail.css('top', 0);
            }
        }
        return $(this).addClass('hover');
    }, function () {
        return $(this).removeClass('hover');
    });
    $('.nav-left').hover(function () {
        if (!$(this).hasClass('show')) {
            return $(this).find('ul').show();
        }
    }, function () {
        if (!$(this).hasClass('show')) {
            return $(this).find('ul').hide();
        }
    });

    $('#cq').closest("li").mouseenter(refreshCart);
    refreshCartHtml();
});
//refresh cart for ajax request
function refreshCart() {
    Module.request.ajax({
        url: base + "/refreshCart.jhtml",
        beforeSend: function () {
            $('#sidebar .shopping-cart').unbind('mouseenter');
            $('#cq').closest("li").unbind('mouseenter');
        },
        success: function (data) {
            refreshCartHtml();
        },
        complete: function () {
            $('#sidebar .shopping-cart').bind('mouseenter', refreshCart);
            $('#cq').closest("li").bind('mouseenter', refreshCart);
        }
    });
}


//refresh cart not ajax request
function refreshCartHtml() {
    var quantity = Module.cartManager.getCartNumForCookie() || 0;
    $('#sidebar .cart-num').text(quantity);
    //同时更新顶部菜单
    $('#cq').text(quantity);
}